package at.ac.tuwien.prog;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import at.ac.tuwien.prog.command.FlashReadCommand;
import at.ac.tuwien.prog.command.ICommand;
import at.ac.tuwien.prog.entity.Register;
import at.ac.tuwien.prog.memory.IMemoryManager;
import at.ac.tuwien.prog.memory.MemoryConstants;
import at.ac.tuwien.prog.system.IProcessor;
import at.ac.tuwien.prog.system.ISystem;
import at.ac.tuwien.prog.view.MainWindow;

/**
 * Main entry point for the application.
 * 
 * @author christoph.pickl@student.tuwien.ac.at
 */
public class App  {
	
	private static final Log LOG = LogFactory.getLog(App.class);
	
	private final ISystem system;

	private final IProcessor processor;
	
	
    public static void main(String[] args) {
    	LOG.info("Starting up spring context...");
    	
        String[] beanDefinitions = { "/beans.xml" };
        new ClassPathXmlApplicationContext(beanDefinitions);
        
    }
    
    public App(final ISystem system, final IProcessor processor) {
    	this.system = system;
    	this.processor = processor;
    }
    
    /**
     * init method gets invoked by spring.
     */
    public void init() {
    	LOG.debug("init() invoked");
    	
    	// display window
    	final MainWindow mainWindow = this.system.getMainWindow();
    	mainWindow.setLocationRelativeTo(null);
    	mainWindow.setVisible(true);
    	
    	
    	// read first flash block
    	new FlashReadCommand(0, MemoryConstants.PROGRAM_COUNTER).execute(this.system);
    	
    	
    	// execute run loop
    	this.processor.run();
    	
    	
    	LOG.debug("init method ended.");
    }
    
}
